package com.sky.wsp.job.controller;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sky.wsp.job.dao.ProjectMapper;
import com.sky.wsp.job.dao.vo.PcAjaxReqVO;
import com.sky.wsp.job.dao.vo.ProjectEntity;
import com.sky.wsp.job.dao.vo.ProvinceCityVO;
import com.sky.wsp.job.util.ChromeDriverUtil;
import com.sky.wsp.job.util.JsonUtil;
import com.sky.wsp.job.util.ResponseCompressionInterceptor;
import com.sky.wsp.job.util.WebDriverUtil;
import okhttp3.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;


/**
 * 剑鱼标讯 网站爬取
 * https://www.jianyu360.cn/front/pcAjaxReq
 * https://www.jianyu360.cn/article/content/ABCY1xGZTIvLDY4El5lZAckDDMZDSdgV1VjKygZID0gWmdzZlJUCO0%3D.html
 * https://www.jianyu360.cn/publicapply/detail/baseInfo
 */
@RestController
public class ScribeJianYuController {

    @Resource
    private ProjectMapper projectMapper;
    private OkHttpClient.Builder builder = new OkHttpClient().newBuilder().addInterceptor(new ResponseCompressionInterceptor());
    private OkHttpClient client = builder.build();

    private List<ProvinceCityVO> getProvinceCity() {
        URL resource = this.getClass().getClassLoader().getResource("province_city.json");
        File file = new File(resource.getPath());
        TypeReference<List<ProvinceCityVO>> typeReference = new TypeReference<List<ProvinceCityVO>>() {
        };
        List<ProvinceCityVO> pcList = JsonUtil.parseArray(file, typeReference);
        return pcList;
    }

    private String[] getSearchKeys() {
        return new String[]{
                "信创国产化",
                "政务云"
        };
    }

    private String pcAjaxReqParam(String searchvalue, String area, String city, int pageNumber) {
        String content = "pageNumber=" + pageNumber + "&pageSize=50&reqType=bidSearch&searchvalue=" + searchvalue + "&area=" + area + "&subtype=中标,成交&publishtime=fiveyear&selectType=content,title&minprice=&maxprice=&industry=&tabularflag=Y&buyerclass=&buyertel=&winnertel=&notkey=&buyer=&winner=&agency=&fileExists=0&city=" + city + "&searchGroup=0&searchMode=0&wordsMode=0&additionalWords=&district=";
        return content;
    }

    @GetMapping("/testjianyu")
    public void testParse() throws IOException {
        URL resource = this.getClass().getClassLoader().getResource("r.json");
        File file = new File(resource.getPath());
        String body = FileReader.create(file).readString();
        PcAjaxReqVO rsp = JsonUtil.parse(body, new TypeReference<PcAjaxReqVO>() {
        });
        System.out.println(rsp.getList().size());
        for (ProjectEntity projectEntity : rsp.getList()) {
            projectMapper.insert(projectEntity);
        }
    }

    @GetMapping("/jianyu")
    public String jianyu() throws Exception {
        List<ProvinceCityVO> pcList = getProvinceCity();
        String[] searchKeys = getSearchKeys();
        for (ProvinceCityVO pc : pcList) {
            for (String searchKey : searchKeys) {
                int currentPage = 1;
                int nextPage = 1;
                try {
                    nextPage = scribe(searchKey, pc.getLabel(), currentPage);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                while (nextPage > currentPage) {
                    currentPage = nextPage;
                    // 间歇1-5秒
                    Thread.sleep(RandomUtil.randomLong(1000, 5000));
                    try {
                        nextPage = scribe(searchKey, pc.getLabel(), currentPage);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        WebDriverUtil.driverQuit();
        return "OK";
    }

    /**
     * 抓取本页数据并返回下页页码
     */
    private int scribe(String searchKey, String city, int currentPage) throws Exception {
        System.out.println(String.format("begin scribe searchKey: %s, city: %s, currentPage: %s", searchKey, city, currentPage));
        String parma = pcAjaxReqParam(searchKey, city, null, currentPage);
        String body = pcAjaxReq(parma);
        // 发送POST请求
        System.out.println(body);
        PcAjaxReqVO rsp = JsonUtil.parse(body, new TypeReference<PcAjaxReqVO>() {
        });

        if (null != rsp.getAntiVerify()) {
            System.err.println("触发反爬图片校验，无法抓取数据！！！！");
            return -1;
        }

        if (null != rsp && null != rsp.getStatus() && 1 == rsp.getStatus()) {
            List<ProjectEntity> list = rsp.getList();
            for (ProjectEntity projectEntity : list) {
                projectMapper.insert(projectEntity);
            }
        }

        Integer maxNum = rsp.getTotalPage();
        int nextPage = currentPage + 1;
        return nextPage > maxNum ? maxNum : nextPage;
    }

    private String pcAjaxReq(String content) {
        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
        RequestBody body = RequestBody.create(mediaType, content);
        Request request = new Request.Builder()
                .url("https://www.jianyu360.cn/front/pcAjaxReq")
                .post(body)
                .addHeader("Accept", "*/*")
                .addHeader("Accept-Encoding", "gzip, deflate, br, zstd")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Content-Length", "374")
                .addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .addHeader("Cookie", "SESSIONID=13a311d80fea26ea980ea43f004679da8129dab6; JYGuestUID=1797519934213849088; SESSIONID=13a311d80fea26ea980ea43f004679da8129dab6; c__utma=875156445.230637235.281617177.1717397124.1717397124.1; c__utmc=875156445.230637235; Hm_lvt_52c42de35032567eb9d7a24a43c84bda=1717397125; device_uuid=d291feaf-26ee-4508-ba8b-631eaf1d44d5; fid=1ca33894158ae1a9f1b7972776265e8f; JYTrustedId=RQwRXVZMCwQAAEwOFV8ETVQBUwJNWEJZUkIABgRQTAlaRE9FBQkGAEVWSV1RRgMDCQFNX0hW; klcn_value=TVZAV1NFAg==; ud_safe=QllBXQQSUFUHVEEJE1pRFgYHUgRDWBRf; c__utmb=875156445.230637235.1717397124.1717397244.3; Hm_lpvt_52c42de35032567eb9d7a24a43c84bda=1717397245; eid=yZTLgtxcE7SjkWsTyXkxMczISGGo/aZgj/L40OLUf4flpEJ17PDjSHqclXIgobnLFyEz227wL7yv4jlUnFgFfzU1qZV0cwRf4tQZfZFgw3AvDPUA90V5Zm2UiMEyKHE1G3PUAMkwDikiCmx5QBuxfc2wlnCHMsWFTk6kyquUjW8=; userid_secure=O/P+YFmyR8/QVowcKoJszOTi16u1Bznb9saMzLh9q91Mfn9nOgR08OBuNSljK6yEgyOzgnLnN43jnWNM1WEjrUwbpQYHj2b8znLzVzCE8jSxAi5SDQ0zwIP2wHGDPdMwQbl7+OCtCRnsEVRfIpdDdPG7jgAe6nS1Z0ZRvF7J41u5DtBJjrzvjT+iCY6pMVGKr9BhBs4UQ+iAuEkkAirG/LnIkadj5D0TBeSZVVaXL1z8G4up+mWXOsABRC5A9AwGkozOKqQEq4w0IUYxpxMHY4Rz47Sxpg3P/V+ZsIrj8BZgAKzXiYWseCPyXOcteaIBJbPPlsK5xcRXi1wMyi96tSoqKjIwMjQtMDYtMDIgMDA6MDA6MDA=")
                .addHeader("Origin", "https://www.jianyu360.cn")
                .addHeader("Pragma", "no-cache")
                .addHeader("Referer", "https://www.jianyu360.cn/jylab/supsearch/index.html")
                .addHeader("Sec-Ch-Ua", "\"Not A(Brand\";v=\"99\", \"Google Chrome\";v=\"121\", \"Chromium\";v=\"121\"")
                .addHeader("Sec-Ch-Ua-Mobile", "?0")
                .addHeader("Sec-Ch-Ua-Platform", "\"Windows\"")
                .addHeader("Sec-Fetch-Dest", "empty")
                .addHeader("Sec-Fetch-Mode", "cors")
                .addHeader("Sec-Fetch-Site", "same-origin")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("content-type", "application/x-www-form-urlencoded")
                .build();

        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "{}";
    }


    @GetMapping("/jianyu/detail")
    public String zhiLianDetail() {

        IPage<ProjectEntity> page = new Page<>();
        page.setSize(100);
        LambdaQueryWrapper<ProjectEntity> wrapper = new LambdaQueryWrapper<>();
        IPage<ProjectEntity> projectPage = projectMapper.selectPage(page, wrapper);
        while (true) {
            List<ProjectEntity> records = projectPage.getRecords();
            if (records.isEmpty()) {
                break;
            }
            for (ProjectEntity project : records) {
                String _id = project.get_id();
                scribeDetail(_id);
            }
            // 下一页
            page.setCurrent(page.getCurrent() + 1);
            projectPage = projectMapper.selectPage(page, wrapper);
        }

        return "OK";
    }

    public static void scribeDetail(String _id) {
        try {
            Thread.sleep(RandomUtil.randomLong(1000, 2000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        WebDriver driver = ChromeDriverUtil.getDriver();
        driver.get("https://www.jianyu360.cn/article/content/" + _id + ".html");
        
        WebElement detail = driver.findElement(By.className("content-detail-container"));


        System.out.println(detail);
    }
}
